import 'package:flutter/material.dart';
import 'package:myapp/generated/app_strings.dart';

class ProductionProgressPage extends StatefulWidget {
  const ProductionProgressPage({Key? key}) : super(key: key);

  @override
  _ProductionProgressPageState createState() => _ProductionProgressPageState();
}

class _ProductionProgressPageState extends State<ProductionProgressPage> {
  final List<Map<String, dynamic>> _data = List.generate(12, (index) => {
    'vehicleNo': (index + 1).toString(),
    'assembly': {'planned': '', 'actual': ''},
    'mechanical': {'planned': '', 'actual': ''},
    'control': {'planned': '', 'actual': ''},
    'electrical': {'planned': '', 'actual': '', 'target': '', 'rate': ''},
    'department': {'mechanical': {'actual': '', 'target': '', 'rate': ''}, 
                  'control': {'actual': '', 'target': '', 'rate': ''}}
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppStrings.of(context).productionProgressTitle),
        backgroundColor: const Color(0xFF1a237e),
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: SingleChildScrollView(
          child: Column(
            children: [
              _buildTableHeader(context),
              ..._buildTableRows(context),
              _buildTotalRow(context),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTableHeader(BuildContext context) {
    return Container(
      color: Colors.grey[200],
      child: Row(
        children: [
          _buildHeaderCell(AppStrings.of(context).vehicleNumber, width: 80),
          _buildHeaderCell(AppStrings.of(context).phaseNumber, width: 80),
          _buildSectionHeader(context, AppStrings.of(context).assembly),
          _buildSectionHeader(context, AppStrings.of(context).mechanical),
          _buildSectionHeader(context, AppStrings.of(context).control),
          _buildElectricalSection(context),
          _buildDepartmentSection(context),
        ],
      ),
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title) {
    return Column(
      children: [
        Container(
          width: 160,
          height: 40,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
          ),
          child: Text(title),
        ),
        Row(
          children: [
            _buildHeaderCell(AppStrings.of(context).planned, width: 80),
            _buildHeaderCell(AppStrings.of(context).actual, width: 80),
          ],
        ),
      ],
    );
  }

  Widget _buildElectricalSection(BuildContext context) {
    return Column(
      children: [
        Container(
          width: 320,
          height: 40,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
          ),
          child: Text(AppStrings.of(context).electricalAndRRP),
        ),
        Row(
          children: [
            _buildHeaderCell(AppStrings.of(context).planned, width: 80),
            _buildHeaderCell(AppStrings.of(context).actual, width: 80),
            _buildHeaderCell(AppStrings.of(context).target, width: 80),
            _buildHeaderCell(AppStrings.of(context).completionRate, width: 80),
          ],
        ),
      ],
    );
  }

  Widget _buildDepartmentSection(BuildContext context) {
    return Column(
      children: [
        Container(
          width: 480,
          height: 40,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            border: Border.all(color: Colors.grey),
          ),
          child: Text(AppStrings.of(context).departmentProgress),
        ),
        Row(
          children: [
            Column(
              children: [
                Container(
                  width: 240,
                  height: 40,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                  ),
                  child: Text(AppStrings.of(context).mechanical),
                ),
                Row(
                  children: [
                    _buildHeaderCell(AppStrings.of(context).actual, width: 80),
                    _buildHeaderCell(AppStrings.of(context).target, width: 80),
                    _buildHeaderCell(AppStrings.of(context).completionRate, width: 80),
                  ],
                ),
              ],
            ),
            Column(
              children: [
                Container(
                  width: 240,
                  height: 40,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey),
                  ),
                  child: Text(AppStrings.of(context).control),
                ),
                Row(
                  children: [
                    _buildHeaderCell(AppStrings.of(context).actual, width: 80),
                    _buildHeaderCell(AppStrings.of(context).target, width: 80),
                    _buildHeaderCell(AppStrings.of(context).completionRate, width: 80),
                  ],
                ),
              ],
            ),
          ],
        ),
      ],
    );
  }

  List<Widget> _buildTableRows(BuildContext context) {
    return _data.map((rowData) => _buildDataRow(context, rowData)).toList();
  }

  Widget _buildDataRow(BuildContext context, Map<String, dynamic> rowData) {
    return Row(
      children: [
        _buildCell(rowData['vehicleNo'], width: 80),
        _buildCell(rowData['vehicleNo'], width: 80),
        _buildCell(rowData['assembly']['planned'], width: 80),
        _buildCell(rowData['assembly']['actual'], width: 80),
        _buildCell(rowData['mechanical']['planned'], width: 80),
        _buildCell(rowData['mechanical']['actual'], width: 80),
        _buildCell(rowData['control']['planned'], width: 80),
        _buildCell(rowData['control']['actual'], width: 80),
        _buildCell(rowData['electrical']['planned'], width: 80),
        _buildCell(rowData['electrical']['actual'], width: 80),
        _buildCell(rowData['electrical']['target'], width: 80),
        _buildCell(rowData['electrical']['rate'], width: 80),
        _buildCell(rowData['department']['mechanical']['actual'], width: 80),
        _buildCell(rowData['department']['mechanical']['target'], width: 80),
        _buildCell(rowData['department']['mechanical']['rate'], width: 80),
        _buildCell(rowData['department']['control']['actual'], width: 80),
        _buildCell(rowData['department']['control']['target'], width: 80),
        _buildCell(rowData['department']['control']['rate'], width: 80),
      ],
    );
  }

  Widget _buildTotalRow(BuildContext context) {
    return Row(
      children: [
        _buildCell(AppStrings.of(context).total, width: 160, color: Colors.grey[200]),
        ...List.generate(16, (index) => _buildCell('', width: 80)),
      ],
    );
  }

  Widget _buildHeaderCell(String text, {required double width}) {
    return Container(
      width: width,
      height: 40,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey),
        color: Colors.grey[200],
      ),
      child: Text(
        text,
        textAlign: TextAlign.center,
        style: const TextStyle(fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget _buildCell(String text, {required double width, Color? color}) {
    return Container(
      width: width,
      height: 40,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey),
        color: color,
      ),
      child: Text(text),
    );
  }
} 